package com.zhang.authoritycenter.service.impl;


import com.zhang.authoritycenter.common.model.dao.auth.UserBaseDO;
import com.zhang.authoritycenter.common.properties.BaseProperties;
import com.zhang.authoritycenter.service.IRedisService;
import com.zhang.infrastructure.model.constant.redis.RedisConstant;
import com.zhang.redis.util.RedisManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2023-12-25 10:54
 **/
@RequiredArgsConstructor
@Slf4j
@Service
public class RedisServiceImpl implements IRedisService {

    private final RedisManager redisManager;
    private final BaseProperties baseProperties;

    @Override
    public void cacheUser(String username, UserBaseDO one) {
        String key = RedisConstant.USERNAME_PREFIX + username;
        redisManager.keyValue().set(key, one, baseProperties.getTableCacheTime(), TimeUnit.SECONDS);
    }

    @Override
    public void removeUser(String username) {
        String key = RedisConstant.USERNAME_PREFIX + username;
        redisManager.keyValue().delete(key);
    }

    @Override
    public void logout(String token, String userId, long expire) {
        String key = RedisConstant.LOGOUT_PREFIX + token;
        redisManager.keyValue().set(key, userId, expire, TimeUnit.SECONDS);
    }
}
